<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="StyleSheet" type="text/css" href="{%$templates%}/admin.css">

<style type="text/css">
.aj_tr_comment {
	font-family: Tahoma, Arial, Sans-Serif;
	color: #7F7F7F;
	font-size: 9pt;
	font-weight: normal;
	text-decoration: none;
	text-align:justify;
	padding-left: 10px;
	padding-right: 10px;
	padding-top: 0;
	padding-bottom: 0;
}

.aj_table {
	font-family: Tahoma, Arial, Sans-Serif;
	color: #000000;
	font-size: 10pt;
	font-weight: normal;
	text-decoration: none;
	padding-left: 10px;
	padding-right: 10px;
	padding-top: 0;
	padding-bottom: 0;
}

.aj_tr_topic {
	font-family: Tahoma, Arial, Sans-Serif;
	color: #377A00;
	font-size: 12pt;
	font-weight: normal;
	text-decoration: none;
	padding-left: 20px;
	padding-right: 0px;
	padding-top: 1px;
	padding-bottom: 1px;
	background-color: #F1FFDD;
}

.aj_tr_intopic {
	font-family: Tahoma, Arial, Sans-Serif;
	color: #377A00;
	font-size: 10pt;
	font-weight: normal;
	text-decoration: none;
	padding-left: 20px;
	padding-right: 0px;
	padding-top: 1px;
	padding-bottom: 1px;
	background-color: #F1FFDD;
}

.aj_tr_branch {
	background-color: #FFFFFF;
	padding: 1px;
}
</style>

<script language="javascript" type="text/javascript">

var BranchTable;
var CurPosition;
var Action;
var SwitchStatus = 'edit';
var langs = new Array();
{%section loop=$langs name=i%}
langs[{%$smarty.section.i.index%}] = '{%$langs[i]%}';
{%/section%}
var mod_names = new Array();
var mod_title = new Array();
{%foreach from=$modules key=mod item=value name=m%}
mod_names[{%$smarty.foreach.m.index%}] = '{%$mod%}';
mod_title[{%$smarty.foreach.m.index%}] = '{%$value.title%}';
{%/foreach%}

{%$WRAPPER%}

function GetTree(id) {
	CurPosition = id;
	x_GetInTree(id, RenderTable);
}

function RenderParent(result)
{
	var bufferHTML = new Array();
	var bufferCount = 0;
	bufferHTML[bufferCount++] = "<table border='0' cellspacing='1' width='600'>";
	bufferHTML[bufferCount++] = "<tr class='aj_tr_topic'>";
	bufferHTML[bufferCount++] = "<td align='right' valign='middle' width='60'>";
	bufferHTML[bufferCount++] = '<a href=\'\' onclick="GetTree(\'\'); return false;">';
	bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/home-gr.gif' hspace='3' width='14' height='14' border='0' title='Home'></a>";
	if(result.trunk)
	{
		bufferHTML[bufferCount++] = '<a href=\'\' onclick="GetTree(\'';
		bufferHTML[bufferCount++] = result.trunk;
		bufferHTML[bufferCount++] = '\'); return false;">';
		bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/up-gr.gif' hspace='3' width='14' height='14' border='0' title='Up'></a>";
	}
	bufferHTML[bufferCount++] = "</td>";
	bufferHTML[bufferCount++] = "<td align='left' valign='middle' width='300'>&nbsp; ";
	bufferHTML[bufferCount++] = result.title;
	bufferHTML[bufferCount++] = "</td>";
	bufferHTML[bufferCount++] = "<td align='left' valign='middle'>";
	if(result.lock.edit)
	{
		bufferHTML[bufferCount++] = '<a href=\'\' onclick="Edit(';
		bufferHTML[bufferCount++] = result.id;
		bufferHTML[bufferCount++] = '); return false;">';
		bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/pencil.gif' hspace='3' width='16' height='16' border='0' title='Edit'></a>";
	}
	if(result.lock.add)
	{
		bufferHTML[bufferCount++] = '<a href=\'\' onclick="Add(';
		bufferHTML[bufferCount++] = result.id;
		bufferHTML[bufferCount++] = ', \'' + result.module + '\'); return false;">';
		bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/add-c-gr.gif' hspace='3' width='14' height='14' border='0' title='Add'></a>";
	}
	if(result.lock.del)
	{
		bufferHTML[bufferCount++] = '<a href="" onclick="if(window.confirm(\'Вы действительно хотите удалить\\nкатегорию: ';
		bufferHTML[bufferCount++] = result.title;
		bufferHTML[bufferCount++] = '?\') == true) {Drop(';
		bufferHTML[bufferCount++] = result.id;
		bufferHTML[bufferCount++] = ', ' + result.trunk + ');} return false;">';
		bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/delete-c-rd.gif' hspace='3' width='14' height='14' border='0' title='Delete'></a>";
	}
	bufferHTML[bufferCount++] = "</td></tr>";
	bufferHTML[bufferCount++] = "<tr class='aj_tr_comment'><td></td>";
	bufferHTML[bufferCount++] = "<td colspan='2' class='aj_comment'>";
	bufferHTML[bufferCount++] = result.notes;
	bufferHTML[bufferCount++] = "</td></tr>";
	bufferHTML[bufferCount++] = "<tr class='aj_tr_branch'>";
	bufferHTML[bufferCount++] = "<td></td><td colspan='2'>";
	bufferHTML[bufferCount++] = BranchTable;
	bufferHTML[bufferCount++] = "</td></tr></table>";

	document.getElementById('_tree_').innerHTML = bufferHTML.join("");
}

function RenderTable(result)
{
	var bufferHTML = new Array();
	var bufferCount = 0;
	var i = 0;

	if(result[0].id > 0)
	{
		bufferHTML[bufferCount++] = "<table border='0' cellspacing='1' width='520'><tbody>";
		for (var i in result)
		{
			bufferHTML[bufferCount++] = "<tr class='aj_tr_topic'>";
			bufferHTML[bufferCount++] = "<td align='right' valign='middle' width='40'>";
			if(result[i].branch)
			{
				bufferHTML[bufferCount++] = '<a href=\'\' onclick="GetTree(\''
				bufferHTML[bufferCount++] = result[i].id ;
				bufferHTML[bufferCount++] = '\'); return false;">';
				bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/in-gr.gif' hspace='3' width='14' height='14' border='0'></a>";
			}
			bufferHTML[bufferCount++] = "</td>";
			bufferHTML[bufferCount++] = "<td align='left' valign='middle' width='300' class='lmid'>&nbsp; ";
			bufferHTML[bufferCount++] = result[i].title;
			bufferHTML[bufferCount++] = "</td>";
			bufferHTML[bufferCount++] = "<td align='left' valign='middle' class='lmid'>";
			if(result[i].lock.edit)
			{
				bufferHTML[bufferCount++] = '<a href=\'\' onclick="Edit(';
				bufferHTML[bufferCount++] = result[i].id;
				bufferHTML[bufferCount++] = '); return false;">';
				bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/pencil.gif' hspace='3' width='16' height='16' border='0' title='Edit'></a>";
			}
			if(result[i].lock.add == true && result[i].allow_depth != 0)
			{
				bufferHTML[bufferCount++] = '<a href=\'\' onclick="Add(';
				bufferHTML[bufferCount++] = result[i].id;
				bufferHTML[bufferCount++] = ', \'' + result[i].module + '\'); return false;">';
				bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/add-c-gr.gif' hspace='3' width='14' height='14' border='0' title='Add'></a>";
			}
			if(result[i].lock.del)
			{
				bufferHTML[bufferCount++] = '<a href="" onclick="if(window.confirm(\'Вы действительно хотите удалить\\nкатегорию: ';
				bufferHTML[bufferCount++] = result[i].title;
				bufferHTML[bufferCount++] = '?\') == true) {Drop(';
				bufferHTML[bufferCount++] = result[i].id;
				bufferHTML[bufferCount++] = ', false);} return false;">';
				bufferHTML[bufferCount++] = "<img src='{%$templates%}/img/delete-c-rd.gif' hspace='3' width='14' height='14' border='0' title='Delete'></a>";
			}
			bufferHTML[bufferCount++] = "&nbsp; &nbsp; &nbsp; &nbsp; ID:" + result[i].id + "</td>";
			bufferHTML[bufferCount++] = "</tr>";
			bufferHTML[bufferCount++] = "<tr class='aj_tr_comment'>";
			bufferHTML[bufferCount++] = "<td></td>";
			bufferHTML[bufferCount++] = "<td colspan='2' class='text'>";
			bufferHTML[bufferCount++] = "<p align='justify' style='color: #7F7F7F; font-size: xx-small'>";
			bufferHTML[bufferCount++] = result[i].notes;
			bufferHTML[bufferCount++] = "</p>";
			bufferHTML[bufferCount++] = "</td></tr>";
			i++;
		}
		bufferHTML[bufferCount++] = "</tbody></table>";
	}

	if(result[0].trunk)
	{
		BranchTable = bufferHTML.join("");
		x_GetNode(result[0].trunk, RenderParent);
	} else
	{
		document.getElementById('_tree_').innerHTML = bufferHTML.join("");
	}
}

function DrowFilledForm(result)
{
	DrowEditorForm(result.id, result.module);
	for(i=0; i<langs.length; i++)
	{
		document.getElementById('title_' + langs[i]).value = eval("result.trans.title." + langs[i]);
		document.getElementById('notes_' + langs[i]).value = eval("result.trans.notes." + langs[i]);
	}
}

function DrowEditorForm(id, module)
{
	if(mod_names.length > 0)
	{
		var bufferHTML = new Array();
		var bufferCount = 0;

		bufferHTML[bufferCount++] = '<form name="node" onsubmit="HandleData(); return false;">';
		bufferHTML[bufferCount++] = "<input type='hidden' name='id' id='id' value='" + id + "'>";
		bufferHTML[bufferCount++] = "<table border='0' width='520'>";
		bufferHTML[bufferCount++] = "<tr class='aj_tr_topic'>";
		bufferHTML[bufferCount++] = "<td colspan='2' align='center'>Добавить / Редактировать категорию:</td></tr>";
		bufferHTML[bufferCount++] = "<tr class='aj_tr_intopic'><td align='right'>Модуль:</td>";
		bufferHTML[bufferCount++] = "<td align='left'>";
		if(module != '')
		{
			bufferHTML[bufferCount++] = "<b>" + module + "</b>";
			bufferHTML[bufferCount++] = "<input type='hidden' name='module' id='module' value='" + module + "'>";
		} else
		{
			bufferHTML[bufferCount++] = "<select name='module'>";
			for(i=0; i<mod_names.length; i++)
			{
				bufferHTML[bufferCount++] = "<option value='" + mod_names[i] + "'>" + mod_title[i] + "</option>";
			}
			bufferHTML[bufferCount++] = "</select>";
		}
		if(langs.length > 0)
		{
			for(i=0; i<langs.length; i++)
			{
				bufferHTML[bufferCount++] = "<tr class='aj_tr_intopic'><td colspan='2' align='left' style='padding-left:20px;'>";
				bufferHTML[bufferCount++] = "Язык: <span class='warn'><b>" + langs[i] + "</b></span></td></tr>";

				bufferHTML[bufferCount++] = "<tr class='aj_tr_intopic'>";
				bufferHTML[bufferCount++] = "<td align='right'>Заголовок:</td>";
				bufferHTML[bufferCount++] = "<td align='left'><input type='text' class='form_f' name='title_";
				bufferHTML[bufferCount++] = langs[i] + "' id='title_" + langs[i] + "' style='width: 250px'></td></tr>";

				bufferHTML[bufferCount++] = "</td></tr>";
				bufferHTML[bufferCount++] = "<tr class='aj_tr_intopic'>";
				bufferHTML[bufferCount++] = "<td align='right' valign='top'>Примечание:</td>";
				bufferHTML[bufferCount++] = "<td align='left'>";
				bufferHTML[bufferCount++] = "<textarea rows='3' cols='35' name='notes_";
				bufferHTML[bufferCount++] = langs[i] + "' id='notes_" + langs[i] + "'></textarea>";
				bufferHTML[bufferCount++] = "</td></tr>";
			}
		}
		bufferHTML[bufferCount++] = "<tr class='aj_tr_intopic'><td></td><td align='left'>";
		bufferHTML[bufferCount++] = '<input type="button" value="&nbsp;Сохранить&nbsp;" onclick="HandleData();"> &nbsp; ';
		bufferHTML[bufferCount++] = '<input type="button" value="&nbsp;Отменить&nbsp;" onclick="GetTree(\'';
		bufferHTML[bufferCount++] = CurPosition + '\');">';
		bufferHTML[bufferCount++] = "</td></tr></table></form>";

		document.getElementById('_tree_').innerHTML = bufferHTML.join("");
	} else
	{
		window.alert('There isn\'t installed modules!');
		return false;
	}
}

function Edit(id)
{
	Action = 'edit';
	x_GetNode(id, DrowFilledForm);
}

function Drop(id, UpLevel)
{
	if(UpLevel != false)
	{
		CurPosition = UpLevel;
	}
	Action = 'drop';
	x_ProcessingData('id=' + id + ':::action=drop', SavedData);
}

function Add(id, module)
{
	Action = 'add';
	DrowEditorForm(id, module);
}

function HandleData()
{
	var output = '';
	output += 'id=' + document.getElementById('id').value + ':::';
	if(document.getElementById('module').value == '')
	{
		window.alert('The module of dependence \nis not specified!');
		return false;
	}
	output += 'module=' + document.getElementById('module').value + ':::';
	//output += 'notes=' + document.getElementById('notes').value + ':::';
	//output += 'visible=' + document.getElementById('visible').value + ':::';
	for(i=0; i<langs.length; i++)
	{
		if(document.getElementById('title_' + langs[i]).value == '')
		{
			window.alert('The title of section for language: ' + langs[i] + '\n is not specified!');
			field = eval( "document.node.title_" + langs[i] );
			field.focus();
			return false;
		}
		str = document.getElementById('title_' + langs[i]).value;
		output += 'title_' + langs[i] + '=' + str.replace(/=/i,'') + ':::';
		str = document.getElementById('notes_' + langs[i]).value;
		output += 'notes_' + langs[i] + '=' + str.replace(/=/i,'') + ':::';
	}
	output += 'action=' + Action;

	x_ProcessingData(output, SavedData);
}

function SavedData(result)
{
	if(result[0] == 0)
	{
		if(Action == 'add') window.alert('Data have not been \nsuccessfully saved!');
		if(Action == 'drop') window.alert('');
	}
	GetTree(CurPosition);
}

function ViewAllTree()
{
	if(SwitchStatus == 'edit')
	{
		SwitchStatus = 'view';
		document.getElementById('switch_view').innerHTML = "В режим редактора";
		x_GetInTree('', DisplayTree);
	} else if (SwitchStatus == 'view')
	{
		SwitchStatus = 'edit';
		document.getElementById('switch_view').innerHTML = "Просмотреть все дерево";
		GetTree('');
	}
}

function DisplayTree(result)
{
	var HTML = RenderTree(result, 0);
	document.getElementById('_tree_').innerHTML = HTML;
}

function RenderTree(result, depth)
{
	var bufferHTML = new Array();
	var bufferCount = 0;
	var i = 0;

	if(result[0].id > 0)
	{
		for (var i in result)
		{
			bufferHTML[bufferCount++] = RenderDepth(depth);
			bufferHTML[bufferCount++] = "<a href='' onclick='Edit(" + result[i].id + "); return false;'>" + result[i].title + "</a><br>";
			if(result[i].branch)
			{
				bufferHTML[bufferCount++] = RenderTree(result[i].branch, depth+1);
			}
			i++;
		}

		return bufferHTML.join("");
	}
}

function RenderDepth(depth)
{
	var HTML = '';
	for(i=0; i<depth; i++)
	{
		HTML += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}

	return HTML;
}
</script>
</head>
<body onload="GetTree('')">

<table border='0' cellpadding='0' cellspacing='1' width='100%'>
<tr>
<td width='150px' style='border-right: solid 1px #ffcc44' valign='top'>

{%include file='admin.meccano-nav.tpl.html'%}

</td>
<td align='left' valign='top' style='padding-left: 20px; padding-top: 10px'>

<span class="ln" onclick="ViewAllTree();" id='switch_view'>Просмотреть все дерево</span>

<br><br>
<div id='_tree_' name='_tree_' class='lmid'>
</div>
</td>
</tr>
</table>
</body>
</html>
